package com.pig4cloud.pigx.strategy.runner;

import com.pig4cloud.pigx.common.customize.redis.utils.RedisUtils;
import com.pig4cloud.pigx.strategy.executor.contract.task.StrategyTaskContractHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * redis任务运行队列监听器的启动器
 *
 * @since 2022/12/13 17:52
 */
@Slf4j
@Component
@RequiredArgsConstructor
@Deprecated
public class StrategyTaskRunListenerRunner implements ApplicationRunner {

    private final StrategyTaskContractHandler strategyTaskContractHandler;
    private final RedisUtils redisUtils;
    private final RedisTemplate redis;
    private long taskCount = 0;

    /**
     * 启动循环执行线程
     *
     * @param args args
     * @since 2022/12/13 17:53
     */
    @Override
    public void run(ApplicationArguments args) {
//        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
//        service.scheduleAtFixedRate(() -> {
//            long l1 = System.currentTimeMillis();
//            String taskId = redisUtils.getMsg(RedisKeyEnum.STRATEGY_TASK_CONTRACT_HANDLE_QUEUE, String.class);
//            if (Strings.isBlank(taskId)) {
//                return;
//            }
//
//            String key = "taskHandleCount";
//            long increment = redis.opsForValue().increment(key, 1);
//            redisUtils.setExpireIfNotSet(key, 1, TimeUnit.SECONDS);
//            if (increment > taskCount) {
//                taskCount = increment;
//            } else {
//                System.err.println("每秒处理" + taskCount + "次");
//                taskCount = increment;
//            }
//
//            System.err.println("耗时：" + (System.currentTimeMillis() - l1));
//            // strategyTaskContractHandler.run(Long.valueOf(taskId));
//        }, 0, 1, TimeUnit.MILLISECONDS);
    }

}
